package com.ruoyi.mobile.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.mobile.domain.*;
import com.ruoyi.mobile.mapper.*;
import com.ruoyi.mobile.utils.*;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.req.ShBannerReq;
import com.ruoyi.system.api.req.SysDeptReq;
import com.ruoyi.system.api.req.SysUserReq;
import com.ruoyi.system.api.resp.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.service.ISysDeptService;

import javax.annotation.Resource;

/**
 * 商铺管理 服务实现
 * 
 * @author ruoyi
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<ISysDeptMapper, SysDeptEntity> implements ISysDeptService {
    @Autowired
    private ISysUserMapper sysUserMapper;
    @Autowired
    private IShProductMapper productMapper;
    @Autowired
    private IShBannerMapper bannerMapper;
    @Autowired
    private IShBannerPictureMapper bannerPicMapper;
    @Autowired
    private MaterialInfojkMapper materialInfojkMapper;
    @Resource
    private StringRedisTemplate myRedisTemplate;

    @Override
    public NftResponse<Paginator<SysDeptResp>> getShopList(SysDeptReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        Paginator<SysDeptResp> paginator = new Paginator<SysDeptResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<SysDeptEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.ne(QueryUtil.camelToUnderline("parentId"), "0");
        wrapperQuery.eq(QueryUtil.camelToUnderline("status"), "0");
        wrapperQuery.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        if (null!=query.getProductType()&&query.getProductType()==0) {
            wrapperQuery.inSql("dept_id", "SELECT shop_id FROM sh_product WHERE product_type<=80");
        }else if (null!=query.getProductType()&&query.getProductType()==-1) {
            wrapperQuery.inSql("dept_id", "SELECT shop_id FROM sh_product WHERE product_type>90");
        }else if (null!=query.getProductType()&&query.getProductType()==-2) {
            wrapperQuery.inSql("dept_id", "SELECT shop_id FROM sh_product WHERE product_type>80 and product_type<=90");
        }else {
            wrapperQuery.inSql("dept_id", "SELECT shop_id FROM sh_product WHERE product_type="+query.getProductType());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
        }
        wrapperQuery.orderByAsc("order_num");
        IPage<SysDeptEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<SysDeptEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<SysDeptEntity> list = pageRtn.getRecords();
        List<SysDeptResp> result = BeanUtil.copyToList(list, SysDeptResp.class);
        for (SysDeptResp obj:result) {
            QueryWrapper<ShProductEntity> wrapperQueryM = new QueryWrapper<>();
            wrapperQueryM.eq(QueryUtil.camelToUnderline("shopId"), obj.getDeptId());
            if (null!=query.getProductType()&&query.getProductType()==0) {
                wrapperQueryM.le(QueryUtil.camelToUnderline("productType"), 80);
            }else if (null!=query.getProductType()&&query.getProductType()==-1) {
                wrapperQueryM.gt(QueryUtil.camelToUnderline("productType"), 90);
            }else if (null!=query.getProductType()&&query.getProductType()==-2) {
                wrapperQueryM.gt(QueryUtil.camelToUnderline("productType"), 80);
                wrapperQueryM.le(QueryUtil.camelToUnderline("productType"), 90);
            }else if (null!=query.getProductType()&&query.getProductType()>0) {
                wrapperQueryM.eq(QueryUtil.camelToUnderline("productType"), query.getProductType());
            }
            Long mcount = this.productMapper.selectCount(wrapperQueryM);
            if (null!=query.getProductType()&&query.getProductType()>0&&query.getProductType()<=80) {
                obj.setProductCount(mcount);
            }else if (null!=query.getProductType()&&query.getProductType()>90) {
                obj.setMealCount(mcount);
            }else if (null!=query.getProductType()&&query.getProductType()>80&&query.getProductType()<=90) {
                obj.setTrainCount(mcount);
            }else {
                QueryWrapper<ShProductEntity> wrapperQueryMT = new QueryWrapper<>();
                wrapperQueryMT.eq(QueryUtil.camelToUnderline("shopId"), obj.getDeptId());
                wrapperQueryMT.gt(QueryUtil.camelToUnderline("productType"), 80);
                wrapperQueryMT.le(QueryUtil.camelToUnderline("productType"), 90);
                Long tcount = this.productMapper.selectCount(wrapperQueryMT);
                obj.setTrainCount(tcount);
                QueryWrapper<ShProductEntity> wrapperQueryMM = new QueryWrapper<>();
                wrapperQueryMM.eq(QueryUtil.camelToUnderline("shopId"), obj.getDeptId());
                wrapperQueryMM.gt(QueryUtil.camelToUnderline("productType"), 90);
                mcount = this.productMapper.selectCount(wrapperQueryMM);
                obj.setMealCount(mcount);
                QueryWrapper<ShProductEntity> wrapperQueryMP = new QueryWrapper<>();
                wrapperQueryMP.eq(QueryUtil.camelToUnderline("shopId"), obj.getDeptId());
                wrapperQueryMP.le(QueryUtil.camelToUnderline("productType"), 80);
                Long pcount = this.productMapper.selectCount(wrapperQueryMP);
                obj.setProductCount(pcount);
            }
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Paginator<ShBannerResp>> getBannerList(ShBannerReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        Paginator<ShBannerResp> paginator = new Paginator<ShBannerResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<ShBannerEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
        }
        wrapperQuery.orderByAsc("order_num");
        IPage<ShBannerEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<ShBannerEntity> pageRtn = this.bannerMapper.selectPage(page,wrapperQuery);
        List<ShBannerEntity> list = pageRtn.getRecords();
        List<ShBannerResp> result = BeanUtil.copyToList(list, ShBannerResp.class);
        for (ShBannerResp obj:result) {
            QueryWrapper<ShBannerPictureEntity> wrapperQueryP = new QueryWrapper<>();
            wrapperQueryP.eq(QueryUtil.camelToUnderline("bannerId"), obj.getId());
            List<ShBannerPictureEntity> picList = this.bannerPicMapper.selectList(wrapperQueryP);
            List<ShBannerPictureResp> resultPic = BeanUtil.copyToList(picList, ShBannerPictureResp.class);
            obj.setBannerPics(resultPic);
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<ShBannerResp> getBannerInfo(Long id) {
        ShBannerEntity entity = this.bannerMapper.selectById(id);
        ShBannerResp rtn = BeanUtil.copyProperties(entity, ShBannerResp.class);
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=rtn.getProductTitle()&&!"".equals(rtn.getProductTitle())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("version"), rtn.getProductTitle());
        }
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        BigDecimal amount = BigDecimal.valueOf(0);
        if (null!=listJk&&listJk.size()>0) {
            for (MaterialInfojkEntity entityJk:listJk) {
                if (null != entityJk.getAmount()) {
                    amount.add(entityJk.getAmount());
                }
            }
            rtn.setJkNum(listJk.size());
            rtn.setAmount(amount);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }

    @Override
    public NftResponse<Paginator<SysDeptResp>> getShopAllList(SysDeptReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictPList = DictUtils.getDictCache("province");
        HashMap<String, String> dictPMap = new HashMap<>();
        HashMap<String, String> dictCMap = new HashMap<>();
        HashMap<String, String> dictRMap = new HashMap<>();
        for (SysDictData data:dictPList) {
            dictPMap.put(data.getDictValue(),data.getDictLabel());
            List<SysDictData> dictCList = DictUtils.getDictCache("city:" + data.getDictValue().substring(0, 2));
            if (null!=dictCList) {
                for (SysDictData datac:dictCList) {
                    dictCMap.put(datac.getDictValue(),datac.getDictLabel());
                    List<SysDictData> dictRList = DictUtils.getDictCache("region:" + datac.getDictValue().substring(0, 4));
                    if (null!=dictRList) {
                        for (SysDictData datar:dictRList) {
                            dictRMap.put(datar.getDictValue(),datar.getDictLabel());
                        }
                    }
                }
            }
        }
        List<SysDeptEntity> records = new ArrayList<>();
        Map<Object, Object> mapValue = myRedisTemplate.opsForHash().entries((null==query.getAppid()?"":query.getAppid())+"SHOP:DEPT");
        if (mapValue.isEmpty()) {
            QueryWrapper<SysDeptEntity> wrapperQuery = new QueryWrapper<>();
            wrapperQuery.ne(QueryUtil.camelToUnderline("parentId"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("status"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("delFlag"),"0");
            if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
                wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
            }
            wrapperQuery.orderByAsc("order_num");
            List<SysDeptEntity> list = this.baseMapper.selectList(wrapperQuery);
            mapValue = new HashMap<>();
            for (SysDeptEntity obj:list) {
                mapValue.put(obj.getDeptId()+"", JSON.toJSONString(obj));
            }
            myRedisTemplate.opsForHash().putAll((null==query.getAppid()?"":query.getAppid())+"SHOP:DEPT", mapValue);
        }
        for (Map.Entry<Object, Object> entry : mapValue.entrySet()) {
            SysDeptEntity entity = JSON.parseObject(entry.getValue().toString().replaceAll("\\\\",""), SysDeptEntity.class);
            records.add(entity);
        }
        List<SysDeptResp> pageResult = BeanUtil.copyToList(records, SysDeptResp.class);
        for (SysDeptResp resp:pageResult) {
            QueryWrapper<ShProductEntity> wrapperQueryT = new QueryWrapper<>();
            wrapperQueryT.eq(QueryUtil.camelToUnderline("shopId"), resp.getDeptId());
            wrapperQueryT.gt(QueryUtil.camelToUnderline("productType"), 80);
            wrapperQueryT.le(QueryUtil.camelToUnderline("productType"), 90);
            Long tcount = this.productMapper.selectCount(wrapperQueryT);
            QueryWrapper<ShProductEntity> wrapperQueryP = new QueryWrapper<>();
            wrapperQueryP.eq(QueryUtil.camelToUnderline("shopId"), resp.getDeptId());
            wrapperQueryP.le(QueryUtil.camelToUnderline("productType"), 80);
            Long pcount = this.productMapper.selectCount(wrapperQueryP);
            QueryWrapper<ShProductEntity> wrapperQueryM = new QueryWrapper<>();
            wrapperQueryM.eq(QueryUtil.camelToUnderline("shopId"), resp.getDeptId());
            wrapperQueryM.gt(QueryUtil.camelToUnderline("productType"), 90);
            Long mcount = this.productMapper.selectCount(wrapperQueryM);
            resp.setProductCount(pcount);
            resp.setMealCount(mcount);
            resp.setTrainCount(tcount);
            resp.setProvince(dictPMap.get(resp.getProvince()));
            resp.setCity(dictCMap.get(resp.getCity()));
            resp.setRegion(dictRMap.get(resp.getRegion()));
            if (null==resp.getLongitude()) {
                resp.setLongitude(new BigDecimal(query.getLng()));
            }
            if (null==resp.getLatitude()) {
                resp.setLatitude(new BigDecimal(query.getLat()));
            }
            double distance = PlatPunchUtil.exceedDistance(resp.getLongitude().toString(),
                    resp.getLatitude().toString(),
                    query.getLng(),
                    query.getLat());
            resp.setDistance(distance);
        }
        Paginator<SysDeptResp> paginator = new Paginator<SysDeptResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        pageResult = pageResult.stream().sorted(Comparator.comparingDouble(SysDeptResp::getDistance)).collect(Collectors.toList());
        Integer start = (query.getPageNo()-1)*query.getPageSize();
        Integer end = query.getPageNo()*query.getPageSize();
        List<SysDeptResp> rtnList = pageResult.subList(start>pageResult.size()?pageResult.size():start,end>pageResult.size()?pageResult.size():end);
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageResult.size());
        paginator.setPageData(rtnList);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Paginator<SysDeptResp>> getTrainAllList(SysDeptReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        List<SysDictData> dictPList = DictUtils.getDictCache("province");
        HashMap<String, String> dictPMap = new HashMap<>();
        HashMap<String, String> dictCMap = new HashMap<>();
        HashMap<String, String> dictRMap = new HashMap<>();
        for (SysDictData data:dictPList) {
            dictPMap.put(data.getDictValue(),data.getDictLabel());
            List<SysDictData> dictCList = DictUtils.getDictCache("city:" + data.getDictValue().substring(0, 2));
            if (null!=dictCList) {
                for (SysDictData datac:dictCList) {
                    dictCMap.put(datac.getDictValue(),datac.getDictLabel());
                    List<SysDictData> dictRList = DictUtils.getDictCache("region:" + datac.getDictValue().substring(0, 4));
                    if (null!=dictRList) {
                        for (SysDictData datar:dictRList) {
                            dictRMap.put(datar.getDictValue(),datar.getDictLabel());
                        }
                    }
                }
            }
        }
        List<SysDeptEntity> records = new ArrayList<>();
        Map<Object, Object> mapValue = myRedisTemplate.opsForHash().entries("SHOPT:DEPT");
        if (mapValue.isEmpty()) {
            QueryWrapper<SysDeptEntity> wrapperQuery = new QueryWrapper<>();
            wrapperQuery.ne(QueryUtil.camelToUnderline("parentId"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("status"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("delFlag"),"0");
            if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
                wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
            }
            wrapperQuery.orderByAsc("order_num");
            List<SysDeptEntity> list = this.baseMapper.selectList(wrapperQuery);
            mapValue = new HashMap<>();
            for (SysDeptEntity obj:list) {
                mapValue.put(obj.getDeptId()+"", JSON.toJSONString(obj));
            }
            myRedisTemplate.opsForHash().putAll("SHOPT:DEPT", mapValue);
        }
        for (Map.Entry<Object, Object> entry : mapValue.entrySet()) {
            SysDeptEntity entity = JSON.parseObject(entry.getValue().toString().replaceAll("\\\\",""), SysDeptEntity.class);
            records.add(entity);
        }
        List<SysDeptResp> pageResult = BeanUtil.copyToList(records, SysDeptResp.class);
        for (SysDeptResp resp:pageResult) {
            QueryWrapper<ShProductEntity> wrapperQueryP = new QueryWrapper<>();
            wrapperQueryP.eq(QueryUtil.camelToUnderline("shopId"), resp.getDeptId());
            wrapperQueryP.le(QueryUtil.camelToUnderline("productType"), 80);
            Long pcount = this.productMapper.selectCount(wrapperQueryP);
            QueryWrapper<ShProductEntity> wrapperQueryT = new QueryWrapper<>();
            wrapperQueryT.eq(QueryUtil.camelToUnderline("shopId"), resp.getDeptId());
            wrapperQueryT.gt(QueryUtil.camelToUnderline("productType"), 80);
            wrapperQueryT.le(QueryUtil.camelToUnderline("productType"), 90);
            Long tcount = this.productMapper.selectCount(wrapperQueryT);
            resp.setProductCount(pcount);
            resp.setTrainCount(tcount);
            resp.setProvince(dictPMap.get(resp.getProvince()));
            resp.setCity(dictCMap.get(resp.getCity()));
            resp.setRegion(dictRMap.get(resp.getRegion()));
            if (null==resp.getLongitude()) {
                resp.setLongitude(new BigDecimal(query.getLng()));
            }
            if (null==resp.getLatitude()) {
                resp.setLatitude(new BigDecimal(query.getLat()));
            }
            double distance = PlatPunchUtil.exceedDistance(resp.getLongitude().toString(),
                    resp.getLatitude().toString(),
                    query.getLng(),
                    query.getLat());
            resp.setDistance(distance);
        }
        Paginator<SysDeptResp> paginator = new Paginator<SysDeptResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        pageResult = pageResult.stream().sorted(Comparator.comparingDouble(SysDeptResp::getDistance)).collect(Collectors.toList());
        Integer start = (query.getPageNo()-1)*query.getPageSize();
        Integer end = query.getPageNo()*query.getPageSize();
        List<SysDeptResp> rtnList = pageResult.subList(start>pageResult.size()?pageResult.size():start,end>pageResult.size()?pageResult.size():end);
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageResult.size());
        paginator.setPageData(rtnList);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<SysDeptResp> getShopInfoById(Long deptId) {
        SysDeptEntity enity = this.baseMapper.selectById(deptId);
        SysDeptResp shopResp = BeanUtil.copyProperties(enity, SysDeptResp.class);
        if (shopResp == null) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "查无此学校", null);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", shopResp);
    }

    @Override
    public NftResponse<HashMap<Long, SysDeptResp>> getSchoolAllMap(SysDeptReq query) {
        List<SysDeptEntity> records = new ArrayList<>();
        Map<Object, Object> mapValue = myRedisTemplate.opsForHash().entries("SHOPT:DEPT");
        if (mapValue.isEmpty()) {
            QueryWrapper<SysDeptEntity> wrapperQuery = new QueryWrapper<>();
            wrapperQuery.ne(QueryUtil.camelToUnderline("parentId"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("status"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("delFlag"),"0");
            if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
                wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
            }
            wrapperQuery.orderByAsc("order_num");
            List<SysDeptEntity> list = this.baseMapper.selectList(wrapperQuery);
            mapValue = new HashMap<>();
            for (SysDeptEntity obj:list) {
                mapValue.put(obj.getDeptId()+"", JSON.toJSONString(obj));
            }
            myRedisTemplate.opsForHash().putAll("SHOPT:DEPT", mapValue);
        }
        for (Map.Entry<Object, Object> entry : mapValue.entrySet()) {
            SysDeptEntity entity = JSON.parseObject(entry.getValue().toString().replaceAll("\\\\",""), SysDeptEntity.class);
            records.add(entity);
        }
        HashMap<Long, SysDeptResp> schoolMap = new HashMap<>();
        List<SysDeptResp> pageResult = BeanUtil.copyToList(records, SysDeptResp.class);
        for (SysDeptResp resp:pageResult) {
            schoolMap.put(resp.getDeptId(), resp);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", schoolMap);
    }

    @Override
    public NftResponse<List<SysDeptResp>> getShopListByPhone(SysUserReq query) {
        QueryWrapper<SysUserEntity> wrapperQueryUser = new QueryWrapper<>();
        wrapperQueryUser.eq(QueryUtil.camelToUnderline("phonenumber"), query.getPhone());
        wrapperQueryUser.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        wrapperQueryUser.eq(QueryUtil.camelToUnderline("status"), "0");
        List<SysUserEntity> userList = sysUserMapper.selectList(wrapperQueryUser);
        List<Long> deptIds = new ArrayList<>();
        for (SysUserEntity obj:userList) {
            deptIds.add(obj.getDeptId());
        }
        Map<Object, Object> mapValue = myRedisTemplate.opsForHash().entries("SHOPT:DEPT");
        List<SysDeptEntity> list = new ArrayList<>();
        if (mapValue.isEmpty()) {
            QueryWrapper<SysDeptEntity> wrapperQuery = new QueryWrapper<>();
            wrapperQuery.ne(QueryUtil.camelToUnderline("parentId"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("status"), "0");
            wrapperQuery.eq(QueryUtil.camelToUnderline("delFlag"),"0");
            wrapperQuery.orderByAsc("order_num");
            list = this.baseMapper.selectList(wrapperQuery);
            Map<String, String> schoolDMap = new HashMap<>();
            for (SysDeptEntity obj:list) {
                schoolDMap.put(obj.getDeptId()+"", JSON.toJSONString(obj));
            }
            myRedisTemplate.opsForHash().putAll("SHOPT:DEPT", schoolDMap);
        }else {
            for (Map.Entry<Object, Object> entry : mapValue.entrySet()) {
                SysDeptEntity entity = JSON.parseObject(entry.getValue().toString().replaceAll("\\\\",""), SysDeptEntity.class);
                list.add(entity);
            }
        }
        List<SysDeptResp> result = BeanUtil.copyToList(list, SysDeptResp.class);
        for (SysDeptResp obj:result) {
            if (deptIds.contains(obj.getDeptId())) {
                obj.setIsMe(true);
            }else {
                obj.setIsMe(false);
            }
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", result);
    }
}
